package com.victor.controller.partner_controller;

import java.net.URLEncoder;
import java.util.List;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.victor.entity.Log;
import com.victor.entity.happy.Happy;
import com.victor.entity.happy.HappyPhoto;
import com.victor.entity.happy.HappyPhotoGroup;
import com.victor.entity.happy.HappyTheme;
import com.victor.entity.happy.HappyThemePhoto;
import com.victor.entity.happy.HappyThemeTime;
import com.victor.entity.happy.HappyType;
import com.victor.service.PartnersService;
import com.victor.utils.DateUtils;
import com.victor.utils.Msg;

/**
 * 娱乐
 * 
 * @author zhangbin
 *
 */
@RestController
@RequestMapping("/partner/happy")
public class Happy_C {

	@Autowired
	private PartnersService service;

	// 获取娱乐商家列表--通过分类id和搜索内容
	@RequestMapping("/getHappyList")
	public Msg getHappyList(String t_id, String search) {
		List<Happy> happys = service.getHappyList(t_id, search);
		return Msg.success().add("happys", happys);
	}

	// 查询娱乐商家详情
	@RequestMapping("/getHappyXQ")
	public Msg getHappyXQ(String h_id) {
		Happy happy = service.getHappyXQ(h_id);
		if (happy != null) {

			return Msg.success().add("happy", happy);
		}
		return Msg.fail("未查询到相关信息");
	}

	// 删除评论
	@RequestMapping("/delComment")
	public Msg delComment(Integer c_id, String adminid) {
		boolean delResult = service.delHappyComment(c_id);
		if (delResult) {
			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("删除了娱乐商家的评论");

			return Msg.success();
		}
		return Msg.fail("未查询到相关信息");
	}

	// 批量导出
	@RequestMapping("batchExport")
	public void batchExport(String adminid, String t_id, String search, HttpServletResponse response,
			HttpServletRequest request) throws Exception {
		// 获取娱乐商家列表--通过分类id和搜索内容
		List<Happy> happys = service.getHappyList(t_id, search);

		// 创建poi导出数据对象
		SXSSFWorkbook sxssfWorkbook = new SXSSFWorkbook();
		// 创建sheet页
		Sheet sheet = sxssfWorkbook.createSheet("娱乐商家列表");

		CellRangeAddress region1 = new CellRangeAddress(0, 1, (short) 0, (short) 6);
		// 参数1：起始行 参数2：终止行 参数3：起始列 参数4：终止列
		sheet.addMergedRegion(region1);
		Row headTitle = sheet.createRow(0);
		headTitle.createCell(0).setCellValue("娱乐商家列表");

		// 创建表头
		Row headRow = sheet.createRow(4);
		// 设置表头信息
		headRow.createCell(0).setCellValue("id");
		headRow.createCell(1).setCellValue("名称");
		headRow.createCell(2).setCellValue("主题");
		headRow.createCell(3).setCellValue("位置--全");
		headRow.createCell(4).setCellValue("位置--省");
		headRow.createCell(5).setCellValue("位置--市");
		headRow.createCell(6).setCellValue("位置--区");
		headRow.createCell(7).setCellValue("位置--街道");
		headRow.createCell(8).setCellValue("位置--详细");
		headRow.createCell(9).setCellValue("位置备注");
		headRow.createCell(10).setCellValue("商家介绍");
		headRow.createCell(11).setCellValue("商家介绍视频");
		headRow.createCell(12).setCellValue("是否为首页推荐 0否 1是");
		headRow.createCell(13).setCellValue("娱乐商家类型表id");
		headRow.createCell(14).setCellValue("创建人id");
		headRow.createCell(15).setCellValue("是否冻结标志 0.正常 1.冻结");
		headRow.createCell(16).setCellValue("冻结起始时间");
		headRow.createCell(17).setCellValue("冻结终止时间");
		headRow.createCell(18).setCellValue("冻结周期 0.终身冻结 1.一小时, 2.一天, 3.一周, 4.一个月,5.一年,6.二年,7.三年,");
		headRow.createCell(19).setCellValue("冻结原由标题");
		headRow.createCell(20).setCellValue("冻结原由详情");

		// 遍历上面数据库查到的数据
		for (Happy happy : happys) {
			Row dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
			dataRow.createCell(0).setCellValue(happy.getH_id());
			dataRow.createCell(1).setCellValue(happy.getH_name());
			dataRow.createCell(2).setCellValue(happy.getH_theme());
			dataRow.createCell(3).setCellValue(happy.getH_position());
			dataRow.createCell(4).setCellValue(happy.getH_position_province());
			dataRow.createCell(5).setCellValue(happy.getH_position_city());
			dataRow.createCell(6).setCellValue(happy.getH_position_area());
			dataRow.createCell(7).setCellValue(happy.getH_position_street());
			dataRow.createCell(8).setCellValue(happy.getH_position_detailed());
			dataRow.createCell(9).setCellValue(happy.getH_position_remarks());
			dataRow.createCell(10).setCellValue(happy.getH_introduce());
			dataRow.createCell(11).setCellValue(happy.getH_video());
			dataRow.createCell(12).setCellValue(happy.getH_ifhomepage());
			dataRow.createCell(13).setCellValue(happy.getT_id());
			dataRow.createCell(14).setCellValue(happy.getAdminid());
			dataRow.createCell(15).setCellValue(happy.getH_frozen());
			dataRow.createCell(16).setCellValue(happy.getH_frozen_starttime());
			dataRow.createCell(17).setCellValue(happy.getH_frozen_endtime());
			dataRow.createCell(18).setCellValue(happy.getH_frozen_cycle());
			dataRow.createCell(19).setCellValue(happy.getH_frozen_title());
			dataRow.createCell(20).setCellValue(happy.getH_frozen_details());

		}
		// 下载导出文件名
		String filename = "娱乐商家列表";
		// 设置头信息
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/vnd.ms-excel");
		// 一定要设置成xlsx格式
		response.setHeader("Content-Disposition",
				"attachment;filename=" + URLEncoder.encode(filename + ".xlsx", "UTF-8"));
		// 创建一个输出流
		ServletOutputStream outputStream = response.getOutputStream();
		// 写入数据
		sxssfWorkbook.write(outputStream);
		// 关闭
		outputStream.close();
		sxssfWorkbook.close();

		// 添加操作日志
		Log log = new Log();
		log.setDate(DateUtils.getNowDate());
		log.setTime(DateUtils.getNowTime());
		log.setAdminid(adminid);
		log.setContent("批量导出娱乐商家列表");
	}

	// 添加娱乐商家
	@RequestMapping("/addHappy")
	public Msg addHappy(Happy happy, String adminid) {
		boolean insResult = service.addHappy(happy);
		if (insResult) {
			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("添加娱乐商家");

			return Msg.success();
		}
		return Msg.fail();
	}

	// 删除美食商家
	@RequestMapping("/delHappy")
	public Msg delHappy(Integer h_id, String adminid) {
		boolean delResult = service.delHappy(h_id);
		if (delResult) {
			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("删除娱乐商家");

			return Msg.success();
		}
		return Msg.fail();
	}

	// 获取所有类型
	@RequestMapping("/getHappyType")
	public Msg getHappyType() {
		List<HappyType> happyTypes = service.getHappyType();
		return Msg.success().add("happyTypes", happyTypes);
	}

	// 修改娱乐商家
	@RequestMapping("/updHappy")
	public Msg updHappy(Happy happy, String adminid) {
		boolean updResult = service.updHappy(happy);
		if (updResult) {
			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("修改娱乐商家信息");
			return Msg.success();
		}
		return Msg.fail();
	}
	// 设置-取消 首页推荐 updHappy方法传id和推荐字段

	// 添加图片组
	@RequestMapping("/addHappyPhotoGroup")
	public Msg addHappyPhotoGroup(HappyPhotoGroup happyPhotoGroup, String adminid) {

		boolean insResult = service.addHappyPhotoGroup(happyPhotoGroup);
		if (insResult) {
			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("娱乐商家添加图片组");
			return Msg.success();
		}
		return Msg.fail();
	}

	// 组内添加图片
	@RequestMapping("/addHappyPhoto")
	public Msg addHappyPhoto(HappyPhoto happyPhoto, String adminid) {

		boolean insResult = service.addHappyPhoto(happyPhoto);
		if (insResult) {
			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("娱乐商家添加图片");
			return Msg.success();
		}
		return Msg.fail();
	}

	// 删除组
	@RequestMapping("/delHappyPhotoGroup")
	public Msg delHappyPhotoGroup(Integer g_id, String adminid) {
		boolean delResult = service.delHappyPhotoGroup(g_id);
		if (delResult) {
			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("删除娱乐商家介绍图片组");
			return Msg.success();
		}
		return Msg.fail();
	}

	// 删除图片
	@RequestMapping("/delHappyPhoto")
	public Msg delHappyPhoto(String p_id, String adminid) {
		boolean delResult = service.delHappyPhoto(p_id);
		if (delResult) {
			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("删除娱乐商家介绍图片");
			return Msg.success();
		}
		return Msg.fail();
	}

	// 获取娱乐项目详情
	@RequestMapping("/getHappyThemeXQ")
	public Msg getHappyThemeXQ(String t_id) {
		HappyTheme happyTheme = service.getHappyThemeXQ(t_id);

		return Msg.success().add("happyTheme", happyTheme);
	}

	// 删除娱乐项目
	@RequestMapping("/delTheme")
	public Msg delTheme(Integer t_id, String adminid) {

		boolean delResult = service.delTheme(t_id);
		if (delResult) {
			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("删除娱乐项目");
			return Msg.success();
		}
		return Msg.fail();
	}

	// 添加娱乐项目
	@RequestMapping("/addTheme")
	public Msg addTheme(@RequestBody HappyTheme happyTheme, @RequestParam String adminid) {
		// 添加娱乐项目返回id
		boolean insResult = service.addTheme(happyTheme);
		if (insResult) {
			// 添加图片list
			if (happyTheme.getHappyThemePhotos() != null && happyTheme.getHappyThemePhotos().size() > 0) {
				for (HappyThemePhoto happyThemePhoto : happyTheme.getHappyThemePhotos()) {
					happyThemePhoto.setT_id(String.valueOf(happyTheme.getT_id()));
					service.addHappyThemePhoto(happyThemePhoto);
				}
			}
			// 添加时间表list
			if (happyTheme.getHappyThemeTimes() != null && happyTheme.getHappyThemeTimes().size() > 0) {
				for (HappyThemeTime happyThemeTime : happyTheme.getHappyThemeTimes()) {
					happyThemeTime.setT_id(String.valueOf(happyTheme.getT_id()));
					service.addHappyThemeTime(happyThemeTime);
				}
			}
			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("添加了娱乐项目");

			return Msg.success();
		}

		return Msg.fail("添加房间信息失败");
	}

	// 修改娱乐项目
	@RequestMapping("/updTheme")
	public Msg updTheme(HappyTheme happyTheme, String adminid) {
		boolean updResult = service.updTheme(happyTheme);

		if (updResult) {
			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("修改娱乐项目");
			return Msg.success();
		}
		return Msg.fail();
	}

	// 添加娱乐项目图片
	@RequestMapping("/addThemePhoto")
	public Msg addThemePhoto(HappyThemePhoto happyThemePhoto, String adminid) {

		boolean insResult = service.addThemePhoto(happyThemePhoto);
		if (insResult) {
			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("添加娱乐项目图片");
			return Msg.success();
		}
		return Msg.fail();
	}

	// 删除娱乐项目图片
	@RequestMapping("/delThemePhoto")
	public Msg delThemePhoto(Integer tp_id, String adminid) {

		boolean delResult = service.delThemePhoto(tp_id);
		if (delResult) {
			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("删除娱乐项目图片");
			return Msg.success();
		}
		return Msg.fail();
	}

	// 添加娱乐项目时间段
	@RequestMapping("/addThemeTime")
	public Msg addThemeTime(HappyThemeTime happyThemeTime, String adminid) {

		boolean insResult = service.addThemeTime(happyThemeTime);
		if (insResult) {
			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("添加娱乐项目时间");
			return Msg.success();
		}
		return Msg.fail();
	}

	// 删除娱乐项目时间段
	@RequestMapping("/delThemeTime")
	public Msg delThemeTime(Integer ti_id, String adminid) {

		boolean delResult = service.delThemeTime(ti_id);
		if (delResult) {
			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("删除娱乐项目时间");
			return Msg.success();
		}
		return Msg.fail();
	}

	// 冻结
	// frozen_title;// 冻结原由标题
	// frozen_details;// 冻结原由详情
	// frozen_starttime;//冻结开始时间
	// frozen_endtime;//冻结结束时间
	// frozen_cycle;// 冻结周期 0.终身冻结 1.1小时, 2.1天, 3.一周, 4.一个月,5.一年,6.二年,7.三年,
	@RequestMapping("/frozenHappy")
	public Msg frozenHappy(Integer h_id, String adminid, String frozen_cycle, String frozen_starttime,
			String frozen_endtime, String frozen_title, String frozen_details) {

		boolean updResult = service.frozenHappy(h_id, frozen_cycle, frozen_starttime, frozen_endtime, frozen_title,
				frozen_details);
		if (updResult) {

			// 添加操作日志
			Log log = new Log();
			log.setDate(DateUtils.getNowDate());
			log.setTime(DateUtils.getNowTime());
			log.setAdminid(adminid);
			log.setContent("冻结了id为" + h_id + "的娱乐商家");
			return Msg.success();
		}
		return Msg.fail();
	}

}
